package GUI;

import CDB.Subcategoria;
import Negocios.ControlSubcategory;
import java.beans.PropertyVetoException;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author ISCErick
 */
public class AgregarSubcategoria extends javax.swing.JInternalFrame {

    
    public AgregarSubcategoria() {
        initComponents();
        cargarSubcategorias();
    }

    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        panSubCat = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtSubcategoria = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        cmbSubcategorias = new javax.swing.JComboBox();
        btnGuardar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setTitle("Agregar una subcategoria");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/IconosGenerales/category.png"))); // NOI18N

        jLabel1.setText("En esta ventana usted puede agregar una nueva subcategoria.");

        panSubCat.setBackground(new java.awt.Color(255, 255, 255));
        panSubCat.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        jLabel2.setText("Nombre de la subcategoria:");

        jLabel4.setText("Acontinuacion se muestra una lista de las subcategorias");

        jLabel5.setText("que se encuentran actualmente registradas en la BD.");

        cmbSubcategorias.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        cmbSubcategorias.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbSubcategoriasActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panSubCatLayout = new javax.swing.GroupLayout(panSubCat);
        panSubCat.setLayout(panSubCatLayout);
        panSubCatLayout.setHorizontalGroup(
            panSubCatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panSubCatLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panSubCatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtSubcategoria)
                    .addGroup(panSubCatLayout.createSequentialGroup()
                        .addGroup(panSubCatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(cmbSubcategorias, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        panSubCatLayout.setVerticalGroup(
            panSubCatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panSubCatLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtSubcategoria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                .addComponent(cmbSubcategorias, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IconosGenerales/save.png"))); // NOI18N
        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IconosGenerales/Cancel.png"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IconosGenerales/category.png"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(panSubCat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnGuardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar))))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnCancelar, btnGuardar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(panSubCat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnGuardar)
                    .addComponent(btnCancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cargarSubcategorias() {
        ControlSubcategory cs = new ControlSubcategory();
        List subcategorias = cs.selectAll();
        for (int i = 0; i < subcategorias.size(); i++) {
            Subcategoria sub = (Subcategoria) subcategorias.get(i);
            cmbSubcategorias.addItem(sub.getNombre());
        }
        cmbSubcategorias.setSelectedIndex(-1);
    }
    
    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        try {
            this.setClosed(true);
            System.gc();
        } catch (PropertyVetoException ex) {
            this.dispose();
            System.gc();
        }
    }//GEN-LAST:event_btnCancelarActionPerformed
    
    private void cmbSubcategoriasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbSubcategoriasActionPerformed
        txtSubcategoria.requestFocus();
        cmbSubcategorias.setSelectedIndex(-1);
    }//GEN-LAST:event_cmbSubcategoriasActionPerformed
    private boolean revisaDuplicado(String subcategoria) {
        ControlSubcategory cs = new ControlSubcategory();
        if (cs.selectOneByName(subcategoria) != null) {
            return true;
        }
        return false;
    }
    
    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        if (revisaDuplicado(txtSubcategoria.getText())) {
            txtSubcategoria.setText("");
            JOptionPane.showMessageDialog(this, "Se ha encontrado una subcategoria con el mismo nombre", "Error de duplicados", 0);
        } else {
            try {
                ControlSubcategory csc = new ControlSubcategory();
                csc.add(txtSubcategoria.getText());
                cmbSubcategorias.removeAllItems();
                cargarSubcategorias();
                txtSubcategoria.setText("");
                txtSubcategoria.requestFocus();
                JOptionPane.showMessageDialog(this, "¡Subcategoria guardada con exito!", "Guardado", 1);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Error: " + e, "Ha ocurrido un erro", 0);
            }
        }
    }//GEN-LAST:event_btnGuardarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JComboBox cmbSubcategorias;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel panSubCat;
    private javax.swing.JTextField txtSubcategoria;
    // End of variables declaration//GEN-END:variables
}
